package com.dehui.traders.ui.me;


import android.content.Context;
import android.databinding.ObservableField;
import android.text.TextUtils;
import android.view.View;

import com.dehui.traders.BuildConfig;
import com.dehui.traders.bean.BaseResponse;
import com.dehui.traders.bean.UserDetail;
import com.dehui.traders.config.Preferences;
import com.dehui.traders.service.ApiUrl;
import com.dehui.traders.service.CallListener;
import com.dehui.traders.service.NetImpl;
import com.dehui.traders.ui.dialog.SharePopView;
import com.dehui.traders.ui.msg.MsgListActivity;
import com.dehui.traders.ui.setting.SettingActivity;
import com.dehui.traders.ui.usercenter.UserCenterActivity;
import com.dehui.traders.ui.web.WebDetailActivity;
import com.dehui.traders.ui.optioanl.MyOptiaonalStockFragemnt;
import com.dehui.traders.utils.UserHelp;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.wechat.friends.Wechat;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.ToastUtils;
import retrofit2.Call;
import retrofit2.Response;

public class MeViewModel extends BaseViewModel{

    public ObservableField<String> titlekName = new ObservableField<>("");

    public ObservableField<String> userNickName = new ObservableField<>("");

    public BindingCommand meOnClick = new BindingCommand(()->{
        if (UserHelp.getIsLogin(context))
            UserCenterActivity.start(context);
    });

    public BindingCommand settingOnClick = new BindingCommand(()->{
        SettingActivity.start(context);
    });

    public BindingCommand messageOnClick = new BindingCommand(()->{
        MsgListActivity.start(context);
    });

    public BindingCommand aboutOnClick = new BindingCommand(()->{
        WebDetailActivity.start(context,"关于我们", BuildConfig.BASEURL + ApiUrl.web.about);
    });

    public BindingCommand helpOnclick = new BindingCommand(()->{
        WebDetailActivity.start(context,"操作帮助", BuildConfig.BASEURL + ApiUrl.web.help);
    });

    public BindingCommand myOptionalOnClick = new BindingCommand(()->{
        startContainerActivity(MyOptiaonalStockFragemnt.class.getCanonicalName());
    });

    public BindingCommand shareOnClick = new BindingCommand(()->{

        showShareDialog();

    });

    private void showShareDialog() {
        SharePopView sharePopView = new SharePopView(fragment.getActivity());
        sharePopView.show();
    }

    public ObservableField<Integer> nickNameVisity = new ObservableField<>(View.GONE);

    public ObservableField<Integer> loginTIpVisity = new ObservableField<>(View.VISIBLE);
    private MeFragment fragment;
    private UserDetail userDetail;

    public MeViewModel(Context context, MeFragment fragment) {
        super(context);
        this.fragment = fragment;
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    public void initUser() {
        String userId = Preferences.getUserId();
        if (!TextUtils.isEmpty(userId)){
            titlekName.set("用户名");
            nickNameVisity.set(View.VISIBLE);
            loginTIpVisity.set(View.GONE);
            queryUser();
        }else{
            titlekName.set("立即登录");
        }

    }

    private void queryUser() {
        Call<BaseResponse<UserDetail>> call = NetImpl.queryUser(UserHelp.getUserId());
        call.enqueue(new CallListener<BaseResponse<UserDetail>>() {
            @Override
            public void _onResponse(Response<BaseResponse<UserDetail>> response) {
                BaseResponse<UserDetail> body = response.body();
                if (body.status == 200){
                    userDetail = body.result;
                    initUserDetail();
                }
            }

            @Override
            public void _onFailure() {

            }
        });
    }

    private void initUserDetail() {
        if (!TextUtils.isEmpty(userDetail.getPhotoUrl())){
            fragment.setHeadImage(userDetail.getPhotoUrl());
        }
        userNickName.set(userDetail.getUserName());
    }


}
